async function entrance() {
    const token = localStorage.getItem("token");
    if (token !== "") {
      const url = `${config.server}/api/admin/entrance`;
      try {
        const response = await fetch(url, {
          method: "POST",
          body: JSON.stringify({ "token": token }),
          headers: { "Content-Type": "application/json" },
        });
  
        const getdata = await response.json();
        if (response.ok) {
          console.log(getdata);
          displayUserInfo(getdata);
        } else {
          console.log(getdata);
          showError();
        }
      } catch (error) {
        console.error('请求失败:', error);
        showError();
      }
    } else {
      showError();
    }
  }
  
function displayUserInfo(data) {
    console.log(data)
    // 如果请求成功，展示用户信息
    document.getElementById("account").innerText = data.AdminInfo.Account;
    document.getElementById("name").innerText = data.UserInfo.Name;
    document.getElementById("avatar").src = data.UserInfo.AvaURL;
    document.getElementById("admin").innerText=data.UserInfo.IsAdmin ? "管理员":"教师"
    // 显示用户信息部分
    document.getElementById("userInfo").style.display="block"
    document.getElementById("error-message").style.display="none"
  }
  
function showError() {
    // 如果请求失败，显示错误消息
    document.getElementById("userInfo").style.display="none";
    document.getElementById("error-message").style.display="block";
  }
  // 请求新的课程表并尝试展示
async function requestClassTabel(){
  const url=`${config.server}`+"/api/select/classTabel"
  const response=await fetch(url,{
    method:"POST",
    headers: { 'Content-Type': 'application/json' },
  })
  const getdata=await response.json()
  if(response.ok){
      console.log(getdata)
      displayClassTabel(getdata)
  }else{
    console.log("请求课程表失败",getdata)
  }
}
function displayClassTabel(classTabelData) {
    const classTable = document.getElementById("classTable");
    const classTabelDiv = document.getElementById("classTabelDiv");
    classTabelDiv.style.display = "table";
    const thead = classTable.querySelector("thead");  // 获取<thead>中的<tr>
    const tbody = classTable.querySelector("tbody");
    const trWeek=document.createElement("tr")
    const thScheme = document.createElement("th");  // 使用<th>而不是<td>，因为thead里应包含<th>
    thScheme.innerText = "上课时间";
    trWeek.appendChild(thScheme);  // 将th添加到thead中的tr

    thead.appendChild(trWeek)
    // 添加每节课的时间
    for (let i=0;i<`${config.classCount}`;i++){
        const tr=document.createElement("tr");
        const tdScheme=document.createElement("td");
        tdScheme.innerText=`${config.scheme[i].start}`+"到"+`${config.scheme[i].end}`
        tr.appendChild(tdScheme);
        tbody.appendChild(tr);
        // 补全空格的内容
        for (let i = 0; i <classTabelData.ClassInOneDay.length; i++){
            const td=document.createElement("td");
            td.innerText="空闲"
            tr.appendChild(td)
        }
    }
    // 遍历classTabelData并在thead中添加星期的标题
    for (let i = 0; i <classTabelData.ClassInOneDay.length; i++) {
        let th = document.createElement("th");  // 使用<th>而不是<td>，因为thead里应包含<th>
        th.innerText = "星期" + ((classTabelData.NowWeekDay + i) % 7);
        trWeek.appendChild(th);  // 将th添加到thead中的tr
        classTabelData.ClassInOneDay[i].forEach(function(element) {
            const td = document.createElement("td");
            const index = element.ClassInfo.Index
            console.log("第", index + 1, "行")
            const row = tbody.rows[index];
            console.log(row)
            const classCell = row.cells[i + 1];
            classCell.innerHTML = ""; // 清空单元格内容
            const buttonDiv = document.createElement("div");
            buttonDiv.innerText = element.ClassInfo.ClassName; // 设置按钮的文本
            buttonDiv.addEventListener("click", () => {
                // 点击时弹出卡片的功能
                const modalContentDiv = document.getElementById("modalContent")
                const modal = document.getElementById("modal");
                modal.style.display = "block";
                modalContentDiv.innerHTML = "<span id='closeBtn' class='close-btn'>&times;</span><h2>班级详情</h2>";

                // 班级部分
                const classInfoDiv = document.createElement("div");
                classInfoDiv.innerHTML = `
            <h3>班级信息</h3>
            <p><strong>班级名称：</strong>${element.ClassInfo.ClassName}</p>
            <p><strong>上课时间：</strong>星期 ${element.ClassInfo.WeekDay}</p>
            <p><strong>课程 ID：</strong>${element.ClassInfo.CourseId}</p>
            <p><strong>最大人数：</strong>${element.ClassInfo.Max}</p>
        `;
                modalContentDiv.appendChild(classInfoDiv);

                // 课程部分
                const courseInfoDiv = document.createElement("div");
                courseInfoDiv.innerHTML = `
            <h3>课程信息</h3>
            <p><strong>课程名称：</strong>${element.CourseInfo.CourseName}</p>
            <p><strong>课程科目：</strong>${element.CourseInfo.CourseSubject}</p>
            <p><strong>课程介绍：</strong>${element.CourseInfo.Introduction}</p>
            <p><strong>推荐人数：</strong>${element.CourseInfo.RecommendMinNum} - ${element.CourseInfo.RecommendMaxNum}</p>
        `;
                modalContentDiv.appendChild(courseInfoDiv);

                // 教师部分
                const teacherInfoDiv = document.createElement("div");
                teacherInfoDiv.innerHTML = `
            <h3>教师信息</h3>
            <p><strong>教师姓名：</strong>${element.TeacherInfo.UserInfo.Name}</p>
            <p><strong>教师昵称：</strong>${element.TeacherInfo.UserInfo.Nickname}</p>
            <p><strong>签名：</strong>${element.TeacherInfo.UserInfo.Signature}</p>
            <img src="${element.TeacherInfo.UserInfo.AvaURL}" alt="教师头像" style="width: 50px; height: 50px; border-radius: 50%;"/>
        `;
                modalContentDiv.appendChild(teacherInfoDiv);

                // 课程预约情况部分
                const resumeInfoDiv = document.createElement("div");
                resumeInfoDiv.innerHTML = `
            <h3>预约情况</h3>
            <p><strong>已预约人数：</strong>${element.ClassInfo.CheckinNum}</p>
            <p><strong>可预约人数：</strong>${element.ClassInfo.Max - element.ClassInfo.CheckinNum}</p>
        `;
                modalContentDiv.appendChild(resumeInfoDiv);

                // 关闭弹窗功能
                const closeBtn = document.getElementById("closeBtn");
                closeBtn.addEventListener("click", function () {
                    modal.style.display = "none"; // 隐藏弹窗
                });

                // 点击弹窗外部区域关闭弹窗
                window.addEventListener("click", function (event) {
                    if (event.target === modal) {
                        modal.style.display = "none"; // 隐藏弹窗
                    }
                });
            });
            classCell.classList.add("clickable");
            buttonDiv.style.cursor = "pointer"; // 设置光标为点击手形            // 将按钮添加到单元格
            classCell.appendChild(buttonDiv);
        })
    }
}
entrance();
requestClassTabel()